<?php

namespace Wantuch\SurveyBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Wantuch\SurveyBundle\Entity\Invitation;
use Wantuch\SurveyBundle\Form\InvitationType;
use Hip\MandrillBundle\Message as MandrillMessage;
use Hip\MandrillBundle\Dispatcher;

/**
 * Invitation controller.
 *
 */
class InvitationController extends Controller
{

    /**
     * Lists all Invitation entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('WantuchSurveyBundle:Invitation')->findAll();

        return $this->render('WantuchSurveyBundle:Invitation:index.html.twig', array(
            'entities' => $entities,
        ));
    }
    /**
     * Creates a new Invitation entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new Invitation();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $mail=$form["email"]->getData();
                    
            if(!filter_var($mail, FILTER_VALIDATE_EMAIL)){
                throw $this->createNotFoundException('Incorrect email.');
                
            }
            $em = $this->getDoctrine()->getManager();
            $token=md5(date('l jS \of F Y h:i:s A').$form["email"]->getData());
            $entity->setToken($token);
            $em->persist($entity);
            $em->flush();
            
            $server_path="http://mailer.timedo.civ.pl";
            $survey_link=$server_path.$this->generateUrl('product_compare', 
                    array(
                        'survey_id' => $entity->getSurvey()->getId(),
                        'compareStep' => 1,
                        'invitation_id'=>$entity->getId(),
                        'token_hash'=>$entity->getToken()
                        ));
            $dispatcher = $this->get('hip_mandrill.dispatcher');

            $message = new MandrillMessage();

            $message
                ->setFromEmail('ankiety@wojtekwantuch.com')
                ->setFromName('System ankietowy')
                ->addTo('wojtekwantuch+test1@gmail.com')
                ->setSubject('Zaproszenie do wzięcia udziału w krótkiej ankiecie')
                ->setHtml('<html><body style="text-align:center;"><h1>Witaj'
                        . ' '
                        . $entity->getName()
                        .' '
                        . $entity->getSurname()
                        . '</h1><p>'
                        
                        . 'Zapraszamy Cię do wzięcia udziału w krótkiej ankiecie. '
                        . 'Kliknij tutaj, aby przejść do ankiety:<br /><br />'
                        . ' '.$survey_link
                        . '</p></body></html>');

            $dispatcher->send($message);
        
        

            return $this->redirect($this->generateUrl('invitation_show', array('id' => $entity->getId())));
        }

        return $this->render('WantuchSurveyBundle:Invitation:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a form to create a Invitation entity.
     *
     * @param Invitation $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(Invitation $entity)
    {
        $form = $this->createForm(new InvitationType(), $entity, array(
            'action' => $this->generateUrl('invitation_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new Invitation entity.
     *
     */
    public function newAction()
    {
        $entity = new Invitation();
        $form   = $this->createCreateForm($entity);

        return $this->render('WantuchSurveyBundle:Invitation:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Invitation entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('WantuchSurveyBundle:Invitation')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Invitation entity.');
        }
        
        $compsArray=NULL;
        if($entity->getResult()){
            $compsArray=$entity->getResult()->getResultArray();
        }
        

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('WantuchSurveyBundle:Invitation:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
            'compsArray' => unserialize($compsArray),
        ));
    }

    /**
     * Displays a form to edit an existing Invitation entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('WantuchSurveyBundle:Invitation')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Invitation entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('WantuchSurveyBundle:Invitation:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a Invitation entity.
    *
    * @param Invitation $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Invitation $entity)
    {
        $form = $this->createForm(new InvitationType(), $entity, array(
            'action' => $this->generateUrl('invitation_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing Invitation entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('WantuchSurveyBundle:Invitation')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Invitation entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('invitation_edit', array('id' => $id)));
        }

        return $this->render('WantuchSurveyBundle:Invitation:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a Invitation entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('WantuchSurveyBundle:Invitation')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Invitation entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('invitation'));
    }

    /**
     * Creates a form to delete a Invitation entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('invitation_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}
